<?php

namespace App\Models;

use App\Traits\InsertOnDuplicateKey;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Facades\DB;

class ZPituitaryPatientsTag extends Pivot
{
    use InsertOnDuplicateKey;

    protected $table = 'z_pituitary_patients_tag';

    protected $guarded = ['id'];


    /**
     *  获取用户在本院使用的标签
     * @param $userIds
     * @param $hospId
     * @param $deptId
     * @return array
     */
    public static function getUserTags($userIds, $hospId, $deptId)
    {
        if (!is_array($userIds)){
            $userIds = [$userIds];
        }

        return self::query()
            ->from('z_pituitary_patients_tag as ut')
            ->selectRaw('t.id,t.name,ut.patient_id as user_id')
            ->leftJoin('z_pituitary_tags as t', 't.id', '=', 'ut.tag_id')
            ->where(
                [
                    'ut.hospital_id' => $hospId,
                    'ut.department_id' => $deptId,
                ])
            ->whereIn('ut.patient_id', $userIds)
            ->get()
            ->toArray();
    }

    /**
     *  获取对应标签对应的用户
     * @param $hospId
     * @param $deptId
     * @param $tip
     * @return array
     */
    public static function getUserByTags($hospId, $deptId, $tip)
    {

        if (!is_array($tip)){
            $tips = [$tip];
        }else{
            $tips = $tip;
        }
       return ZPituitaryPatientsTag::query()
            ->selectRaw('patient_id')
            ->where('hospital_id', $hospId)
            ->where('department_id', $deptId)
            ->whereIn('tag_id', $tips)
            ->get()->toArray();
    }
    /**
     * 给患者添加标签
     *
     * @param $input
     * @return bool|\Exception|\Throwable
     */
    public static function relateTags($input)
    {
        try {
            if (!isset($input['add_tag_ids'])) $input['add_tag_ids'] = [];
            if (!isset($input['del_tag_ids'])) $input['del_tag_ids'] = [];
            $patientIds = array_unique($input['patient_ids']);
            $addTagIds = array_unique($input['add_tag_ids']);
            $delTagIds = array_unique($input['del_tag_ids']);
            DB::beginTransaction();
            foreach ($patientIds as $patientId) {
                if ($addTagIds) {
                    $insertAttr = [];
                    foreach ($addTagIds as $tagId) {
                        $insertAttr[] = [
                            'patient_id' => $patientId,
                            'tag_id' => $tagId,
                            'hospital_id' => $input['hospital_id'],
                            'department_id' => $input['department_id']
                        ];
                    }

                    ZPituitaryPatientsTag::insertIgnore($insertAttr);
                }

                if ($delTagIds) {
                    ZPituitaryPatientsTag::query()
                        ->where(['patient_id' => $patientId])
                        ->where(['hospital_id' => $input['hospital_id'], 'department_id' => $input['department_id']])
                        ->whereIn('tag_id', $delTagIds)
                        ->delete();
                }
            }

            DB::commit();
            return true;
        } catch (\Throwable $t) {
            DB::rollBack();
            return false;
        }
    }

}
